//
//  ZAQVisitorView.swift
//  ZAQWBDZT
//
//  Created by 赵安庆 on 16/9/20.
//  Copyright © 2016年 zaq. All rights reserved.
//

import UIKit
protocol loginDelegate : NSObjectProtocol{
   func didSelectedButton()
}
class ZAQVisitorView: UIView {
    var loginClosure :(()->())?

    weak var delegate:loginDelegate?
//懒加载加载控件
    private lazy var cycleImageView : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    private lazy var maskImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    private lazy var messageLabel : UILabel  = {

        let label   = UILabel()
        label.text =  "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 14)

        label.textColor = UIColor.darkGray
        label.textAlignment = .center

        label.numberOfLines = 0

        return label

    }()

    private lazy var loginButton : UIButton = {

        let button  = UIButton()
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //  去掉点击的高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()

    // MARK:登录和注册的点击事件

    @objc private func loginAction(){
        print("我是访客视图按钮的点击事件")
        loginClosure?()
      delegate?.didSelectedButton()

    }

    private lazy var registerButton:UIButton = {
        let button = UIButton()

        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //  去掉点击的高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()

    // 重写init方法
    override init(frame:CGRect){
        super.init(frame: frame)
        setupUI()
    }


    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)

        //设置约束
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)

        }
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))


        }
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)

        }
    }

    // 修改访客视图的view方法
    func updateVisitorInfo(message:String?,imageName:String?)  {
        if let msg = message ,let imageName = imageName{
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imageName)
            cycleImageView.isHidden = true
        }else{
            // 不满足上述条件就是首页！，在这里执行动画
            startAnimation()
        }
    }
    // 开启动画
    private func startAnimation() {
        //  创建核心动画对象
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  执行到的目的地
        animation.toValue = 2 * M_PI
        //  动画执行的时长
        animation.duration = 20
        //  动画执行的次数
        animation.repeatCount = MAXFLOAT
        //  失去焦点让其不释放
        animation.isRemovedOnCompletion = false

        cycleImageView.layer.add(animation, forKey: nil)


    }


}
